#!/usr/bin/env python
import numpy as np
import re

def get_upper_wind_d(datestr, hh='00', station='SSPwh'):
    """datestr: YYYYMMDD or filename
    """
    indir = '/media/CCLI_GROUP_L/data/hko_wind_profiler'
    yyyy = datestr[0:4]
    yyyymm = datestr[0:6]
    m = re.match(r'^\d{8}$', datestr)
    if m is None:
        fname = datestr
    else:
        fname = '%s/%s/%s/SSPwh%s%s00' % (indir, yyyy, yyyymm, datestr, hh)
    try:
        res = np.loadtxt(fname, dtype=[('qc', 'i4'), ('height', 'f8'),('ws', 'f4'), ('wd', 'f8'), ('u', 'f8'), ('v', 'f8')], usecols=(0,1,2,3,4,5), skiprows=3, ndmin=1)
        w = np.where(res['qc']!=0)
        res['ws'][w] = np.nan
        res['wd'][w] = np.nan
        res['u'][w] = np.nan
        res['v'][w] = np.nan
    except Exception as e:
        res = None
    return res

